package ytdp.careagle.com.ytdp.myview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import ytdp.careagle.com.ytdp.R;

/**
 * Created by admin on 2017/9/25.
 */

public class MyPayFirstView extends View {

    private Paint paint;
    private int colorGray;
    private int colorSelected;
    //    private DashPathEffect dashPathEffect;
    private Path path;
    private Bitmap progress;
    private float seletedX;
    private boolean isFirst = true;
    private CallBack callBack;

    public MyPayFirstView(Context context) {
        this(context, null);
    }

    public MyPayFirstView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        colorGray = getResources().getColor(R.color.text_gray);
        colorSelected = getResources().getColor(R.color.guestPaint_color_wrong);
        paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(colorGray);
        paint.setStrokeWidth(getResources().getDimension(R.dimen.dp1));
//        dashPathEffect = new DashPathEffect(new float[]{getResources().getDimension(R.dimen.dp20), getResources().getDimension(R.dimen.dp2)}, 1);
//        paint.setPathEffect(dashPathEffect);//虚线
        paint.setAntiAlias(true);
        path = new Path();

        progress = BitmapFactory.decodeResource(getResources(), R.drawable.icon_progress);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                seletedX = x;
                break;
            case MotionEvent.ACTION_MOVE:
                seletedX = x;
                break;
            case MotionEvent.ACTION_UP:
                int i = getWidth() / 4;//1/4
                int v = (int) (seletedX / i);
                float lastValue = seletedX % i;
                if (lastValue > i / 2) {
                    v++;
                    seletedX = v * i;
                } else {
                    seletedX = v * i;
                }
                if (callBack != null) {
                    if (v < 0) {
                        v = 0;
                    }
                    if (v > 4) {
                        v = 4;
                    }
                    callBack.call(v);
                }
                break;
        }
        postInvalidate();
        return true;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        getParent().requestDisallowInterceptTouchEvent(true);
        return super.dispatchTouchEvent(ev);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        path.reset();
        path.moveTo(progress.getWidth() / 2, getHeight() / 2);
        path.lineTo(getWidth() - progress.getWidth() / 2, getHeight() / 2);
        paint.setColor(colorGray);
        canvas.drawPath(path, paint);

        if (seletedX < (progress.getWidth() / 2)) {
            seletedX = progress.getWidth() / 2;
        } else if (seletedX > (getWidth() - progress.getWidth() / 2)) {
            seletedX = getWidth() - progress.getWidth() / 2;
        }
        path.reset();
        path.moveTo(progress.getWidth() / 2, getHeight() / 2);
        path.lineTo(seletedX, getHeight() / 2);
        paint.setColor(colorSelected);
        canvas.drawPath(path, paint);

        paint.setColor(Color.WHITE);
        canvas.drawLine(getWidth() / 4 - 2, getHeight() / 2, getWidth() / 4 + 2, getHeight() / 2, paint);
        canvas.drawLine(getWidth() / 4 * 2 - 2, getHeight() / 2, getWidth() / 4 * 2 + 2, getHeight() / 2, paint);
        canvas.drawLine(getWidth() / 4 * 3 - 2, getHeight() / 2, getWidth() / 4 * 3 + 2, getHeight() / 2, paint);
        canvas.drawBitmap(progress, seletedX - progress.getWidth() / 2, getHeight() / 2 - progress.getHeight() / 2, paint);
    }


    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        super.onWindowFocusChanged(hasWindowFocus);
        if (isFirst && hasWindowFocus) {
            isFirst = false;
            seletedX = progress.getWidth() / 2;
        }
    }

    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }

    public interface CallBack {
        void call(int selected);
    }
}
